package com.jerei.plugin.control;

import com.jerei.plugin.entity.WcmCommonButton;
import com.jerei.plugin.entity.WcmCommonButtonCat;
import com.jerei.plugin.service.IWcmCommonButtonCatService;
import com.jerei.plugin.service.IWcmCommonButtonService;
import com.jerei.utils.UtilsCommon;
import com.jerei.utils.UtilsCondition;
import com.jerei.utils.spring.PageBean;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsUI;
import com.jerei.web.utils.UtilsWeb;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;

/**
 * Created by sunfeihu on 2016/10/21.
 */
@Extension
public class WcmCommonButtonControl extends PluginController {

    @Autowired
    private IWcmCommonButtonService iWcmCommonButtonService;

    @Autowired
    private IWcmCommonButtonCatService iWcmCommonButtonCatService;

    @RequiresPermissions({"common-button:save", "common-button:update"})
    public String rightSave() {
        WcmCommonButton entity = this.wrapper(WcmCommonButton.class);
        if (entity.getId() != null && entity.getId() > 0L) {
            iWcmCommonButtonService.update(entity);
        } else {
            iWcmCommonButtonService.save(entity);
        }
        return UtilsUI.ok();
    }

    @RequiresPermissions({"common-button:save", "common-button:update"})
    public String leftSave() {
        WcmCommonButtonCat entity = this.wrapper(WcmCommonButtonCat.class);
        if (entity.getId() != null && entity.getId() > 0L) {
            iWcmCommonButtonCatService.update(entity);
        } else {
            iWcmCommonButtonCatService.save(entity);
        }
        return UtilsUI.ok();
    }

    @RequiresPermissions({"common-button:remove"})
    public String rightDelete() {
        String[] ids = this.getRequest().getParameterValues("ids");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                iWcmCommonButtonService.delete(UtilsCommon.getLong(ids[i]));
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }

    @RequiresPermissions({"common-button:remove"})
    public String leftDelete() {
        String[] ids = this.getRequest().getParameterValues("ids");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                iWcmCommonButtonCatService.delete(UtilsCommon.getLong(ids[i]));
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }

    @RequiresPermissions({"common-button:save", "common-button:update", "common-button:view"})
    public void leftEdit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        WcmCommonButtonCat entity = null;
        if (null != id) {
            entity = iWcmCommonButtonCatService.find(id);
        } else {
            entity = new WcmCommonButtonCat();
        }
        this.getModel().addAttribute("obj", entity);
    }

    @RequiresPermissions({"common-button:save", "common-button:update", "common-button:view"})
    public void rightEdit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        Long catId = UtilsWeb.getLong(this.getRequest(), "catId");
        WcmCommonButton entity = null;
        if (null != id) {
            entity = iWcmCommonButtonService.find(id);
        } else {
            entity = new WcmCommonButton();
            entity.setCatId(catId);
        }
        this.getModel().addAttribute("obj", entity);
    }

    @RequiresPermissions({"common-button:view"})
    public String rightPage() {
        Long catId = UtilsWeb.getLong(this.getRequest(), "search.eq.catId");
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        if (null != catId) {
            return UtilsUI.datagrid(iWcmCommonButtonService.page(nowPage, pageSize, UtilsCondition.getConditionEntity(this.getRequest().getParameterMap())));
        } else {
            return UtilsUI.datagrid();
        }
    }

    @RequiresPermissions({"common-button:view"})
    public String leftPage() {
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        PageBean<WcmCommonButtonCat> pageBean = iWcmCommonButtonCatService.page(nowPage, pageSize, UtilsCondition.getConditionEntity(this.getRequest().getParameterMap()));
        return UtilsUI.datagrid(pageBean);
    }
}